Class 7 Exam  >  Class 7 Questions  >  What will be the output of the following C++ ... Start Learning for Free
What will be the output of the following C++ code?
#include<iostream>
using namespace std;
 
int &fun()
{
    static int x = 10;
    return x;
}
int main()
{
    fun() = 30;
    cout << fun();
    return 0;
}
  • a)
    30
  • b)
    10
  • c)
    Error
  • d)
    Segmentation fault
Correct answer is option 'A'. Can you explain this answer?
Most Upvoted Answer
What will be the output of the following C++ code?#include<iostream...
A function returning value by reference can be used as lvalue i.e. it can be used on the left side of an expression. Here when we are doing fun() = 30 then we are changing the value of x(i.e. value returning) to and as x is static therefore it will not be initialized again so the value of x becomes 30 hence the output is 30.
Free Test
Community Answer
What will be the output of the following C++ code?#include<iostream...

Explanation:

Static Variable:
- The function `fun` returns a reference to a static variable `x`.
- Static variables are initialized only once and retain their value across function calls.

Main Function:
- In the `main` function, we assign the value `30` to the variable `x` by calling the function `fun()`.
- The statement `fun() = 30;` modifies the value of `x`.

Output:
- The output of the code will be `30` because we modified the value of the static variable `x` to `30` in the `main` function.

Therefore, the correct answer is option 'A) 30'.
Attention Class 7 Students!
To make sure you are not studying endlessly, EduRev has designed Class 7 study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Class 7.
Explore Courses for Class 7 exam

Top Courses for Class 7

What will be the output of the following C++ code?#include<iostream>using namespace std;int &fun(){ static int x = 10; return x;}int main(){ fun() = 30; cout << fun(); return 0;}a)30b)10c)Errord)Segmentation faultCorrect answer is option 'A'. Can you explain this answer?
Question Description
What will be the output of the following C++ code?#include<iostream>using namespace std;int &fun(){ static int x = 10; return x;}int main(){ fun() = 30; cout << fun(); return 0;}a)30b)10c)Errord)Segmentation faultCorrect answer is option 'A'. Can you explain this answer? for Class 7 2024 is part of Class 7 preparation. The Question and answers have been prepared according to the Class 7 exam syllabus. Information about What will be the output of the following C++ code?#include<iostream>using namespace std;int &fun(){ static int x = 10; return x;}int main(){ fun() = 30; cout << fun(); return 0;}a)30b)10c)Errord)Segmentation faultCorrect answer is option 'A'. Can you explain this answer? covers all topics & solutions for Class 7 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What will be the output of the following C++ code?#include<iostream>using namespace std;int &fun(){ static int x = 10; return x;}int main(){ fun() = 30; cout << fun(); return 0;}a)30b)10c)Errord)Segmentation faultCorrect answer is option 'A'. Can you explain this answer?.
Solutions for What will be the output of the following C++ code?#include<iostream>using namespace std;int &fun(){ static int x = 10; return x;}int main(){ fun() = 30; cout << fun(); return 0;}a)30b)10c)Errord)Segmentation faultCorrect answer is option 'A'. Can you explain this answer? in English & in Hindi are available as part of our courses for Class 7. Download more important topics, notes, lectures and mock test series for Class 7 Exam by signing up for free.
Here you can find the meaning of What will be the output of the following C++ code?#include<iostream>using namespace std;int &fun(){ static int x = 10; return x;}int main(){ fun() = 30; cout << fun(); return 0;}a)30b)10c)Errord)Segmentation faultCorrect answer is option 'A'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What will be the output of the following C++ code?#include<iostream>using namespace std;int &fun(){ static int x = 10; return x;}int main(){ fun() = 30; cout << fun(); return 0;}a)30b)10c)Errord)Segmentation faultCorrect answer is option 'A'. Can you explain this answer?, a detailed solution for What will be the output of the following C++ code?#include<iostream>using namespace std;int &fun(){ static int x = 10; return x;}int main(){ fun() = 30; cout << fun(); return 0;}a)30b)10c)Errord)Segmentation faultCorrect answer is option 'A'. Can you explain this answer? has been provided alongside types of What will be the output of the following C++ code?#include<iostream>using namespace std;int &fun(){ static int x = 10; return x;}int main(){ fun() = 30; cout << fun(); return 0;}a)30b)10c)Errord)Segmentation faultCorrect answer is option 'A'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What will be the output of the following C++ code?#include<iostream>using namespace std;int &fun(){ static int x = 10; return x;}int main(){ fun() = 30; cout << fun(); return 0;}a)30b)10c)Errord)Segmentation faultCorrect answer is option 'A'. Can you explain this answer? tests, examples and also practice Class 7 tests.
Explore Courses for Class 7 exam

Top Courses for Class 7

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev